package com.ny.demo524;

import com.ny.view.Users;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    public int addUser(String username, String password) throws SQLException, ClassNotFoundException {
        Connection connection = SqlConnection.getCon();
        String sql = "INSERT INTO users(username, password) VALUES (?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        return preparedStatement.executeUpdate();
    }
    public Users login(String username, String password) throws SQLException, ClassNotFoundException {
        Users users = null;
        Connection connection = SqlConnection.getCon();
        String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            users = new Users();
            users.setId(resultSet.getInt("id"));
            users.setUsername(resultSet.getString("username"));
        }
        return users;
    }
}
